﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfExecutePythonScript
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private const string pythonExePath = @"D:\devtools\Anaconda3\pkgs\python-3.8.5-h5fd99cc_1\python.exe";
        private ImprotInfo improtInfo = new ImprotInfo();
        public MainWindow()
        {
            InitializeComponent();
            grid.DataContext = improtInfo;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //improtInfo.TextBoxInfo += "开始导入";
            var scriptPath = @"E:\python\test.py";
            ImportData(scriptPath, (sender, e) => { improtInfo.TextBoxInfo += e.Data + "\r\n"; });
        }

        private void ImportData(string scriptPath, DataReceivedEventHandler outputDataReceive, DataReceivedEventHandler errorDataReceived = null)
        {

            if (errorDataReceived == null)
            {
                errorDataReceived = outputDataReceive;
            }
            Task.Run(() =>
            {
                improtInfo.CanImport = false;

                try
                {
                    var pythonScriptPath = @$"-u {scriptPath}";//注意要修改py scirpt所在的位置
                    var process = new Process
                    {
                        StartInfo = new ProcessStartInfo
                        {
                            FileName = pythonExePath,
                            Arguments = pythonScriptPath,
                            UseShellExecute = false,
                            RedirectStandardOutput = true,
                            RedirectStandardError = true,
                            CreateNoWindow = true
                        },
                        EnableRaisingEvents = true
                    };
                    process.ErrorDataReceived += errorDataReceived;
                    process.OutputDataReceived += outputDataReceive;

                    process.Start();
                    process.BeginErrorReadLine();
                    process.BeginOutputReadLine();
                    process.WaitForExit();
                }
                catch (Exception ex)
                {
                    improtInfo.TextBoxInfo += ex.Message + "\r\n";
                }
                //告知ui现在可以继续import了
                improtInfo.CanImport = true;
            });

        }
    }
    class ShareRes
    {
        public static int count = 0;
        public static Mutex mutex = new Mutex();
    }


    public class ImprotInfo : INotifyPropertyChanged
    {
        private string _TextBoxInfo;
        public string TextBoxInfo
        {
            get
            {
                return _TextBoxInfo;
            }
            set
            {
                _TextBoxInfo = value;
                OnPropertyChanged("TextBoxInfo");
            }
        }

        private bool _CanImport = true;
        public bool CanImport
        {
            get { return _CanImport; }

            set
            {
                _CanImport = value;
                OnPropertyChanged("CanImport");
            }
        }

        protected internal virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;

    }
}
